package com.example.abe.diandi.trends.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.example.abe.diandi.R;
import com.example.abe.diandi.trends.model.TrendModel;

import example.abe.com.framework.recycleview.base.ItemViewDelegate;
import example.abe.com.framework.recycleview.base.ViewHolder;
import example.abe.com.framework.util.TimeUtil;

import static example.abe.com.framework.util.TimeUtil.WEEK_FORMAT_2;

public abstract class BaseTrendsDelegate implements ItemViewDelegate<TrendModel> {

    private OnClickListener mListener;

    public void setOnClickListener(OnClickListener listener){
        mListener = listener;
    }

    @Override
    public View getItemView(Context context, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_base_trends, parent, false);
        //点击事件绑定
        view.findViewById(R.id.item_base_trends_ll_content).setOnClickListener(onClickListener);
        //设置内容
        FrameLayout flContent = (FrameLayout)view.findViewById(R.id.item_base_trends_fl_content);
        setInnerContent(context, flContent);
        return view;
    }

    @Override
    public void bindViewHolder(ViewHolder holder, TrendModel t, int position){
        //保存当前位置
        holder.setTag(R.id.item_base_trends_ll_content, position);
        //隐藏部分视图
        holder.setVisible(R.id.item_base_trends_iv_time_divider_first, t.isDayFirst());
        holder.setVisible(R.id.item_base_trends_iv_arrow_first, t.isDayFirst());
        holder.setVisible(R.id.item_base_trends_iv_content_divider, !t.isDayFirst());
        holder.setVisible(R.id.item_base_trends_view_place_holder, t.isDayFirst());
        holder.setVisible(R.id.item_base_trends_tv_day, t.isDayFirst());
        holder.setVisible(R.id.item_base_trends_tv_week, t.isDayFirst());
        holder.setVisible(R.id.item_base_trends_tv_year, t.isDayFirst());
        //设置公共属性
        //时间
        holder.setText(R.id.item_base_trends_tv_year, TimeUtil.getStrTimeCH(t.getTime(), "yyyy . MM"));
        holder.setText(R.id.item_base_trends_tv_day, TimeUtil.getStrTimeCH(t.getTime(), "dd"));
        holder.setText(R.id.item_base_trends_tv_time, TimeUtil.getStrTimeCH(t.getTime(), "HH:mm"));
        holder.setText(R.id.item_base_trends_tv_week, TimeUtil.getWeek(t.getTime(), WEEK_FORMAT_2));
        //标题
        holder.setText(R.id.item_base_trends_tv_title, t.getTitle());
        bindDetailViewHolder(holder, t, position);
    }

    /**
     * 添加点击事件
     */
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Object obj = v.getTag();
            if (obj == null){
                return;
            }
            int position = (Integer)obj;
            if (mListener != null){
                mListener.onClick(v, position);
            }
        }
    };

    /**
     * 设置内容
     * @param contentView 内容容器
     * @param context 上下文
     */
    protected abstract void setInnerContent(Context context, FrameLayout contentView);

    /**
     * 为ViewHolder绑定数据
     * @param holder ViewHolder内容
     * @param t 数据
     * @param position 位置
     */
    protected abstract void bindDetailViewHolder(ViewHolder holder, TrendModel t, int position);

    /**
     * 点击事件监听
     */
    public interface OnClickListener {
        /**
         * 点击事件监听
         * @param v 视图
         */
        void onClick(View v, int postion);
    }
}